const mongoose = require('mongoose')
const Schema = mongoose.Schema

const UserSchema = new Schema({
  name: { type: String, unique: true },
  email: { type: String, required: true },
  password: { type: String, required: true },
  sex: { type: Number, default: 0 },
  phone: { type: String },
  explain: { type: String },
  avatar: { type: String },
  level: { type: Number, default: 1 },
  role: { type: Schema.Types.ObjectId, ref: 'Role' },
  result_type: { type: Number, default: 1 }
}, { timestamps: true })

const AdminSchema = new Schema({
  name: { type: String, unique: true, required: true },
  password: { type: String, required: true },
  role: { type: Schema.Types.ObjectId, ref: 'Role' }
}, { timestamps: true })

const RoleSchema = new Schema({
  roleName: { type: String, unique: true, required: true },
  roleNum: { type: String, unique: true, required: true }
}, { timestamps: true })

const CategorySchema = new Schema({
  categoryName: { type: String, unique: true, required: true },
  cate_path: { type: String, unique: true, required: true},
  result_type: { type: Number, default: 4 }
}, { timestamps: true })

const LabelSchema = new Schema({
  categoryId: { type: Schema.Types.ObjectId, ref: 'Category' },
  label: { type: String, unique: true },
  icon: { type: String },
  attention_count: { type: Number, default: 0 },
  article_count: { type: Number, default: 0 },
  result_type: { type: Number, default: 2 }
}, { timestamps: true })

const LabelUserSchame = new Schema({
  labelId: { type: Schema.Types.ObjectId, ref: 'Label' },
  userId: { type: Schema.Types.ObjectId, ref: 'User' }
}, { timestamps: true })

const ArticleSchema = new Schema({
  userId: { type: Schema.Types.ObjectId, ref: 'User' },
  categoryId: { type: Schema.Types.ObjectId, ref: 'Category' },
  labelId: { type: Schema.Types.ObjectId, ref: 'Label' },
  title: { type: String, required: true },
  // author: { type: String, required: true },
  description: { type: String, required: true },
  content: { type: String, required: true },
  hot: { type: Number, default: 0 },
  zan: { type: Number, default: 0 },
  read: { type: Number, default: 0 },
  commentsCount: { type: Number, default: 0 },
  result_type: { type: Number, default: 3 }
}, { timestamps: true })

const CommentSchema = new Schema({
  articleId: { type: Schema.Types.ObjectId, ref: 'Article' },
  userId: { type: Schema.Types.ObjectId, ref: 'User' },
  replayId: { type: Schema.Types.ObjectId, ref: 'User' },
  comments: { type: String, unique: true }
}, { timestamps: true })

const FavoriteSchema = new Schema({
  articleId: { type: Schema.Types.ObjectId, ref: 'Article' },
  userId: { type: Schema.Types.ObjectId, ref: 'User' }
}, { timestamps: true })

const AttentionSchema = new Schema({
  userId: { type: Schema.Types.ObjectId, ref: 'User' /** 被关注人 */ },
  attention: { type: Schema.Types.ObjectId, ref: 'User' /** 关注人 */ }
}, { timestamps: true })

const CollectionAttenSchema = new Schema({
  collectionId: { type: Schema.Types.ObjectId, ref: 'Collection' },
  articleId: { type: Schema.Types.ObjectId, ref: 'Article' },
}, { timestamps: true })

const CollectionSchema = new Schema({
  userId: { type: Schema.Types.ObjectId, ref: 'User' },
  collectionName: { type: String, unique: true, required: true }
}, { timestamps: true })

const ZanSchema = new Schema({
  userId: { type: Schema.Types.ObjectId, ref: 'User' },
  articleId: { type: Schema.Types.ObjectId, ref: 'Article' },
  // zanStatus: {type: Number, default: 1 /** 0:未点赞 1：点赞 */}
}, { timestamps: true })


const UserModel = mongoose.model('User', UserSchema)
const CategoryModel = mongoose.model('Category', CategorySchema)
const LabelModel = mongoose.model('Label', LabelSchema)
const ArticleModel = mongoose.model('Article', ArticleSchema)
const CommentModel = mongoose.model('Comment', CommentSchema)
const FavoriteModel = mongoose.model('Favorite', FavoriteSchema)
const CollectionAttenModel = mongoose.model('CollectionAtten', CollectionAttenSchema)
const AttentionModel = mongoose.model('Attention', AttentionSchema)
const CollectionModel = mongoose.model('Collection', CollectionSchema)
const ZanModel = mongoose.model('zan', ZanSchema)
// const LabelArticleModel = mongoose.model('LabelArticle', LabelArticleSchame)
const LabelUserModel = mongoose.model('LabelUser', LabelUserSchame)
const AdminModel = mongoose.model('Admin', AdminSchema)
const RoleModel = mongoose.model('Role', RoleSchema)

module.exports = {
  UserModel,
  CategoryModel,
  LabelModel,
  ArticleModel,
  CommentModel,
  FavoriteModel,
  CollectionAttenModel,
  AttentionModel,
  CollectionModel,
  ZanModel,
  LabelUserModel,
  AdminModel,
  RoleModel
}